package top.yihoxu.yapi.controller;



import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import top.yihoxu.yapi.common.BaseResponse;
import top.yihoxu.yapi.common.ErrorCode;
import top.yihoxu.yapi.common.ResultUtils;
import top.yihoxu.yapi.exception.BusinessException;
import top.yihoxu.yapi.model.dto.user.UserLoginRequest;
import top.yihoxu.yapi.model.dto.user.UserRegisterRequest;
import top.yihoxu.yapi.model.entity.User;
import top.yihoxu.yapi.model.vo.LoginUserVO;
import top.yihoxu.yapi.model.vo.UserVO;
import top.yihoxu.yapi.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @Author dth
 * @Date 2024/7/15 16:51
 * @PackageName:top.yihoxu.yapi.controller
 * @ClassName: UserController
 * @Description:
 * @Version 1.0
 */
/**
 * 用户接口
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        String codeCaptcha = userLoginRequest.getCodeCaptcha();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword,codeCaptcha, request);
        return ResultUtils.success(loginUserVO);
    }
    /**
     * 用户注销
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }
    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<UserVO> getLoginUser(HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return ResultUtils.success(userVO);
    }
    @GetMapping("/get/captcha")
    public BaseResponse<String> getNewCaptcha() {
        // 创建一个线型验证码实例
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 40, 6, 40); // 宽度、高度、字符和干扰线数量可自定义
        //获取验证码Base64
        String imageBase64 = captcha.getImageBase64();
        String code = captcha.getCode();
        redisTemplate.opsForValue().set("codeCaptcha", code, 3 * 60, TimeUnit.SECONDS);
        return ResultUtils.success(imageBase64);

    }
}
